//
//  SSSAlertWindow.m
//  SSSMerchant
//
//  Created by 谢曦 on 2017/3/25.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSAlertWindow.h"
//static CGFloat contentViewW=280;
//static  CGFloat contentViewH=308+5;
//static  CGFloat contentViewY=148;
//static  CGFloat themePicH=182;
//static CGFloat themePicW=280;
//static CGFloat kmargin=10;
static CGFloat buttonH=51;
@interface SSSAlertWindow ()


@property (strong, nonatomic)  UIView *maskView;//遮罩
@property (nonatomic,strong) UIImageView *contentView;//底层view
@property (nonatomic,strong) UIView *customCenterView;

@end
@implementation SSSAlertWindow
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        [self maskView];
        [self contentView];
        
    }
    return self;
}
#pragma mark - public
+(SSSAlertWindow *)alertWindowWithPicName:(NSString *)picName contentTextArr:(NSArray  <NSAttributedString *>*)textArr themeButtontText:(NSString *)themeButtontText cancleButtonText:(NSString *)cancleButtonText
{
    SSSAlertWindow *alertWindow=[[SSSAlertWindow alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    SSSAlertWindowButton *themeBtn=[SSSAlertWindowButton buttonWithTitle:themeButtontText highLight:YES];
    if (cancleButtonText.length>0) {
          SSSAlertWindowButton *cancleBtn=[SSSAlertWindowButton buttonWithTitle:cancleButtonText highLight:NO];
        [alertWindow configureUIWithWithpicName:picName contentTextArr:textArr buttonArr:@[themeBtn,cancleBtn]];
    }
  
    else{
     [alertWindow configureUIWithWithpicName:picName contentTextArr:textArr buttonArr:@[themeBtn]];
    
    }
    
    
    return alertWindow;


}
+(SSSAlertWindow *)alertWindowWithPicName:(NSString *)picName contentTextArr:(NSArray  <NSAttributedString *> *)textArr buttonArr:(NSArray  <SSSAlertWindowButton *>*)buttonArr
{
    SSSAlertWindow *alertWindow=[[SSSAlertWindow alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [alertWindow configureUIWithWithpicName:picName contentTextArr:textArr buttonArr:buttonArr];
    
    return alertWindow;
    
}

+(instancetype)picAndTextAlertViewWithPicName:(NSString *)picName customCenterView:(UIView *)customCenterView buttonArr:(NSArray  <SSSAlertWindowButton *>*)buttonArr
{
    SSSAlertWindow *alertWindow=[[SSSAlertWindow alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    alertWindow.customCenterView=customCenterView;
    [alertWindow configureUIWithWithpicName:picName contentTextArr:@[] buttonArr:buttonArr];
    
    return alertWindow;
    
}

#pragma mark - private
//默认数组摆放顺序 从右到左
-(void)configureUIWithWithpicName:(NSString *)picName contentTextArr:(NSArray *)textArr buttonArr:(NSArray  <SSSAlertWindowButton *>*)buttonArr{
    UIImageView *pic=[[UIImageView alloc] init];
    pic.image=[UIImage imageNamed:picName];
    [self.contentView addSubview:pic];
    [pic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(pic.image.size.width);
        make.height.mas_equalTo(pic.image.size.height);
        make.centerX.mas_equalTo(self);
    }];
    
    //有几个str 创建几个
    UIView *lastView;
    
    for (NSAttributedString *attr in textArr) {
        UILabel *lable=[[UILabel alloc] init];
        lable.attributedText=attr;
        [lable sizeToFit];
        lastView=[self.contentView.subviews lastObject];
        [self.contentView addSubview:lable];
        [lable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.contentView);
            make.top.mas_equalTo(lastView.mas_bottom).with.offset(5);
        }];
    }
    //_customCenterView
    [self.contentView addSubview:_customCenterView];
    
    [_customCenterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.top.mas_equalTo(pic.mas_bottom).with.offset(15);
        make.size.mas_equalTo(_customCenterView.bounds.size);
        
    }];

    UIView *centerView=[self.contentView.subviews lastObject];
    
    //close
    UIButton *delete=[[UIButton alloc] init];
    [delete setImage:[UIImage imageNamed:@"icon_close"] forState:UIControlStateNormal];
    [self.contentView addSubview:delete];
    
    [delete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(delete.currentImage.size.width);
        make.height.mas_equalTo(delete.currentImage.size.height);
        make.top.mas_equalTo(self.contentView.mas_top).with.offset(15)        ;
        make.right.mas_equalTo(self.contentView.mas_right).with.offset(-15);
    }];
  
    [delete addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //button
    for (int i=0; i<buttonArr.count; i++) {
        SSSAlertWindowButton *button=buttonArr[i];
        [self.contentView addSubview:button];
        button.tag=100+i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }

    if (buttonArr.count>1) {
    [buttonArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
        
        [buttonArr mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(buttonH);
             make.top.equalTo(centerView.mas_bottom).with.offset(30);
        }];

    }
    else if(buttonArr.count==1){
        [buttonArr[0] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(buttonH);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.top.equalTo(centerView.mas_bottom).with.offset(30);
//             make.centerX.equalTo(self.contentView);
        }];
    }
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH-30);
        make.top.equalTo(pic.mas_top).with.offset(pic.image.size.height/2);
        make.center.mas_equalTo(self);
        make.bottom.equalTo(self.contentView.subviews.lastObject.mas_bottom).with.offset(20);
        
    }];

}


#pragma mark - action
-(void)show
{
    UIView *window = [[[UIApplication sharedApplication] delegate] window];
    [window addSubview:self];
    
}

-(void)tapToDismiss:(UITapGestureRecognizer *)tap
{
    
    if(CGRectContainsPoint(self.contentView.frame, [tap locationInView:self.maskView])){
        return;
    }
  else [self dismiss];
}



-(void)dismiss
{
    [self removeFromSuperview];
}

-(void)deleteButtonClick:(UIButton *)button
{
     [self dismiss];
}
-(void)buttonClick:(UIButton *)button
{
    if (self.SSSAlertWindowButtonClick) {
        self.SSSAlertWindowButtonClick(button.tag-100);
    }
    else [self dismiss];
}
-(UIImageView *)contentView
{
    if (!_contentView) {
        _contentView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self.maskView addSubview:_contentView];
                _contentView.backgroundColor=[UIColor whiteColor];
        _contentView.layer.cornerRadius=4;
        _contentView.userInteractionEnabled=YES;
       //
    }
    return _contentView;
    
}
-(UIView *)maskView
{
    if (!_maskView) {
        _maskView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _maskView.backgroundColor= [[UIColor blackColor] colorWithAlphaComponent:0.4];
        [self addSubview:_maskView];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToDismiss:)];
        [_maskView addGestureRecognizer:tap];
    }
    return _maskView;
}


@end
@implementation SSSAlertWindowButton

+(SSSAlertWindowButton *)buttonWithTitle:(NSString *)title highLight:(BOOL)highLighted{
    SSSAlertWindowButton *button=[SSSAlertWindowButton new];
    button.layer.cornerRadius=4;
    [button setTitle:title forState:UIControlStateNormal];
    if (highLighted==YES) {
        button.backgroundColor=ThemeGreenColor;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
   else {
        button.backgroundColor=[UIColor whiteColor];
        [button setTitleColor:ThemeGreenColor forState:UIControlStateNormal];
        button.layer.borderWidth=1;
        button.layer.borderColor=ThemeGreenColor.CGColor;
    }
    return button;
}

@end
